package cn.sunlelin.educationserver.controller;

import cn.sunlelin.educationserver.entity.Fathertype;
import cn.sunlelin.educationserver.service.FathertypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@CrossOrigin
public class FathertypeController {
    @Autowired
    private FathertypeService fathertypeService;

    @PostMapping("/fathertype/createFathertype")
    public Map<String,Object> createFathertype(@RequestBody Fathertype fathertype){
        Map<String,Object> map=new HashMap<>();
        if(fathertypeService.createFathertype(fathertype)){
            map.put("code",200);
            map.put("msg","创建大类成功");
            map.put("dataobject",null);
        }
        else {
            map.put("code",500);
            map.put("msg","创建大类失败，该大类名已存在");
            map.put("dataobject",null);
        }
        return map;
    }

    @PostMapping("/fathertype/updateFathertype")
    public Map<String,Object> updateFathertype(@RequestBody Fathertype fathertype){
        Map<String,Object> map=new HashMap<>();
        if(fathertypeService.updateFathertype(fathertype)){
            map.put("code",200);
            map.put("msg","修改大类成功");
            map.put("dataobject",null);
        }
        else {
            map.put("code",500);
            map.put("msg","修改大类失败，该大类名已存在");
            map.put("dataobject",null);
        }
        return map;
    }

    @GetMapping("/fathertype/findAll")
    public Map<String,Object> findAll(){
        List<Fathertype> fathertypeList=fathertypeService.findAll();
        Map<String,Object> map=new HashMap<>();
        if(fathertypeList.size()!=0){
            map.put("code",200);
            map.put("msg","查询所有大类成功");
            map.put("dataobject",fathertypeList);
        }
        else {
            map.put("code",500);
            map.put("msg","查询大类失败");
            map.put("dataobject",null);
        }
        return map;
    }

    @GetMapping("/fathertype/selectFathertypeById/{fathertypeId}")
    public Map<String,Object> selectFathertypeById(@PathVariable Integer fathertypeId){
        Fathertype fathertype=fathertypeService.selectFathertypeById(fathertypeId);
        Map<String,Object> map=new HashMap<>();
        if(fathertype!=null){
            map.put("code",200);
            map.put("msg","根据Id查找大类成功");
            map.put("dataobject",fathertype);
        }
        else {
            map.put("code",500);
            map.put("msg","根据Id查找大类失败，该大类不存在");
            map.put("dataobject",null);
        }
        return map;
    }

    @GetMapping("/fathertype/selectFathertypeByName")
    public Map<String,Object> selectFathertypeByName(String fathertypeName){
        Fathertype fathertype=fathertypeService.selectFathertypeByName(fathertypeName);
        Map<String,Object> map=new HashMap<>();
        if(fathertype!=null){
            map.put("code",200);
            map.put("msg","根据Name查找大类成功");
            map.put("dataobject",fathertype);
        }
        else {
            map.put("code",500);
            map.put("msg","根据Name查找大类失败，该大类不存在");
            map.put("dataobject",null);
        }
        return map;
    }


    @GetMapping("/fathertype/deleteFathertype/{fathertypeId}")
    public Map<String,Object> deleteFathertype(@PathVariable Integer fathertypeId){
        Map<String,Object> map=new HashMap<>();
        if(fathertypeService.deleteFathertype(fathertypeId)){
            map.put("code",200);
            map.put("msg","删除大类成功");
            map.put("dataobject",null);
        }
        else {
            map.put("code",500);
            map.put("msg","删除大类失败，该大类不存在");
            map.put("dataobject",null);
        }
        return map;
    }
}
